Completed
Push — master ( 866116...4a55ed )
by Ajeh
36s
created

Translator.get   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
dl 0
loc 7
rs 9.4285
nop 1
1
// mini translator
2
3
import en from './translations/en'
4
5
6
const Translator = function (translations, separator = '.') {
7
    let lang = window.navigator.languages[1] || window.navigator.userLanguage[1]
8
    this.lang = typeof translations[lang] !== 'undefined' ? lang : 'en'
9
    this.separator = separator
10
    this.translations = translations
11
}
12
13
Translator.prototype.get = function (route) {
14
    let parts = route.split(this.separator)
15
    let translation = this.translations[this.lang]
16
17
    for (let i = 0; i < parts.length; i++) {
18
        translation = translation[parts[i]]
19
        if (translation === undefined) {
20
            translation = 'No Translation'
21
            break
22
        }
23
    }
24
25
    return translation
26
}
27
28
let T = new Translator({en})
29
30
export default function (n) {
31
    return T.get(n)
32
}